'''
This file contains the image operations that can be performed in AugIm. 

Created on Jan 25, 2010

@author: madsk
'''

import scavenger

@scavenger.cprofilescavenge('len(#0)', store = True)
def blur(image_data, factor = 1.0):
    from PIL import ImageEnhance, Image
    from StringIO import StringIO
    sio = StringIO(image_data)
    pil_image = Image.open(sio)
    factor = 1.0 - factor
    new_image = ImageEnhance.Sharpness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, 'JPEG', quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)', store = True)
def sharpen(image_data, factor = 1.0):
    from PIL import ImageEnhance, Image
    from StringIO import StringIO
    sio = StringIO(image_data)
    pil_image = Image.open(sio)
    factor = 1.0 + factor
    new_image = ImageEnhance.Sharpness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, 'JPEG', quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)', store = True)
def brightness(image_data, factor = 1.0): 
    from PIL import ImageEnhance, Image
    from StringIO import StringIO
    sio = StringIO(image_data)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Brightness(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, 'JPEG', quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)', store = True)
def color(image_data, factor = 1.0): 
    from PIL import ImageEnhance, Image
    from StringIO import StringIO
    sio = StringIO(image_data)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Color(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, 'JPEG', quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)', store = True)
def contrast(image_data, factor = 1.0): 
    from PIL import ImageEnhance, Image
    from StringIO import StringIO
    sio = StringIO(image_data)
    pil_image = Image.open(sio)
    new_image = ImageEnhance.Contrast(pil_image).enhance(factor)
    sio = StringIO()
    new_image.save(sio, 'JPEG', quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)', store = True)
def fliphorizontal(image_data):
    from PIL import Image
    from StringIO import StringIO
    sio = StringIO(image_data)
    pil_image = Image.open(sio)
    new_image = pil_image.transpose(Image.FLIP_TOP_BOTTOM)
    sio = StringIO()
    new_image.save(sio, 'JPEG', quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)', store = True)
def flipvertical(image_data):
    from PIL import Image
    from StringIO import StringIO
    sio = StringIO(image_data)
    pil_image = Image.open(sio)
    new_image = pil_image.transpose(Image.FLIP_LEFT_RIGHT)
    sio = StringIO()
    new_image.save(sio, 'JPEG', quality=95)
    return sio.getvalue()

@scavenger.cprofilescavenge('len(#0)', store = True)
def scale(image_data, size):
    from PIL import Image
    from StringIO import StringIO
    sio = StringIO(image_data)
    pil_image = Image.open(sio)
    new_image = pil_image.resize(size, Image.ANTIALIAS)
    sio = StringIO()
    new_image.save(sio, 'JPEG', quality=95)
    return sio.getvalue()
